package com.greensam.pixelengine.controller;

import com.greensam.pixelengine.annotation.AuthCheck;
import com.greensam.pixelengine.facade.UserFacadeService;
import com.greensam.pixelengine.pojo.base.BaseResponse;
import com.greensam.pixelengine.facade.RoleFacadeService;
import com.greensam.pixelengine.pojo.base.DeleteRequest;
import com.greensam.pixelengine.pojo.emuns.RoleEnum;
import com.greensam.pixelengine.pojo.entity.UserPo;
import com.greensam.pixelengine.pojo.vo.role.request.RoleSaveRequest;
import com.greensam.pixelengine.pojo.vo.role.request.UserRoleSaveRequest;
import com.greensam.pixelengine.pojo.vo.role.response.RolePermissionVo;
import com.greensam.pixelengine.pojo.vo.role.response.RoleVo;
import com.greensam.pixelengine.pojo.vo.user.response.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色模块
 *
 * @author Macro_Ray
 * @since 2025/6/19 16:04
 */
@Tag(name = "RoleController", description = "角色模块")
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleFacadeService roleFacadeService;

    private final UserFacadeService userFacadeService;

    @Operation(summary = "新增|修改角色")
    @PostMapping("/save")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<Boolean> addRole(HttpServletRequest httpServletRequest, @RequestBody @Valid RoleSaveRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(roleFacadeService.saveRole(request, loginUser.getId()));
    }

    @Operation(summary = "角色添加用户")
    @PostMapping("/user/save")
    public BaseResponse<Boolean> addUserRole(HttpServletRequest httpServletRequest, @RequestBody @Valid UserRoleSaveRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(roleFacadeService.saveUserRole(request, loginUser.getId()));
    }

    @Operation(summary = "获取角色列表")
    @GetMapping("/list")
    public BaseResponse<List<RoleVo>> listRole() {
        return BaseResponse.success(roleFacadeService.listRole());
    }

    @Operation(summary = "获取该角色所有用户")
    @GetMapping("/user/list")
    public BaseResponse<List<UserVo>> getUsersByRoleId(@RequestParam("roleId") @Parameter(description = "角色ID") Long roleId) {
        return BaseResponse.success(userFacadeService.getUsersByRoleId(roleId));
    }

    @Operation(summary = "删除角色")
    @PostMapping("/deleted")
    public BaseResponse<Boolean> deleteRole(HttpServletRequest httpServletRequest, @RequestBody @Valid DeleteRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(roleFacadeService.deleteRole(request, loginUser.getId()));
    }

    @Operation(summary = "查询当前用户权限列表")
    @GetMapping("/user/permission/list")
    public BaseResponse<List<RolePermissionVo>> listUserPermission(HttpServletRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(request);
        return BaseResponse.success(roleFacadeService.listUserPermission(loginUser.getId()));
    }

}
